<template>
  <section>
    <div class="filter-container">
      <common-input @keyup.enter.native="handleFilter" class="w200" placeholder="小店名称" v-model="listQuery.keyword" />
      <common-input @keyup.enter.native="handleFilter" class="w200 ml10" placeholder="用户"
        v-model="listQuery.memberKeyword" />
      <!-- <el-date-picker
          class="ml10"
          size="mini"
          v-model="listQuery.contractEnd"
          type="date"
        format="yyyy年MM月dd日"
        value-format="yyyy-MM-dd"
          placeholder="注册时间">
        </el-date-picker> -->
      <el-button size="mini" class="ml10" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button size="mini" class="ml10" @click="handleCreate" type="primary" icon="el-icon-plus">新增</el-button>
    </div>
    <el-tabs size="mini" :before-leave='leaveTab' v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="全部" name="0"></el-tab-pane>
      <el-tab-pane label="已审核" name="2"></el-tab-pane>
      <el-tab-pane label="待审核" name="1"></el-tab-pane>
      <el-tab-pane label="被驳回" name="3"></el-tab-pane>
    </el-tabs>
    <el-table max-height="500" :data="list" style="width: 100%">
      <el-table-column align="left" label="活动名">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column align="left" label="活动地点" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="保证金" width="150">
        <template slot-scope="scope">
          <span>￥{{ scope.row.bondMoney }}</span>
        </template>
      </el-table-column>
      <el-table-column align="right" label="是否支付保证金">
        <template slot-scope="scope">
          <span>{{ scope.row.bondStatusText }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.statusText }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="添加日期" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateAt) }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button type="text">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="openShopDetail(scope.row.id)">详情</el-dropdown-item>

              <el-dropdown-item v-if="scope.row.status == 1"
                @click.native="handleCheck(scope.row)">退款审核</el-dropdown-item>
              <!-- <el-dropdown-item v-if="scope.row.status==1" @click.native="handleCheck2(scope.row)">取消审核</el-dropdown-item> -->
              <!-- <el-dropdown-item v-if="scope.row.status==0&&scope.row.isFront==1" @click.native="handleRefuse(scope.row)">拒绝审核</el-dropdown-item> -->
              <!-- <el-dropdown-item @click.native="handleRecharge(scope.row)">充值</el-dropdown-item> -->
              <el-dropdown-item @click.native="handleDelete(scope.row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="listQuery.page" :page-sizes="[20, 30, 50]" :page-size="listQuery.num"
        layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>

    <el-dialog title="活动详情" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="500px">
      <div>
        <div>活动名称：{{ detail.name }}</div>
        <div>活动地址：{{ detail.address }}</div>
        <div>活动时间：{{ parseTime(detail.startTime) }}至{{ parseTime(detail.endTime) }}</div>
        <div>保证金：{{ detail.bondMoney }}({{ detail.bondStatusText }})</div>

        <div></div>
        <div style="border-top: #ccc solid 1px; margin-top: 10px; padding: 20px 0;">
          <div v-for="item in detail.productList" :key="item.id" style="display: flex; padding-bottom: 20px;">
            <div style="margin-right: 20px;"><img :src="item.productDetail.listImgUrl" width="60" /></div>
            <div>
              {{ item.productDetail.title }}
              <br />
              数量：{{ item.stock }}

            </div>


          </div>
        </div>
        <div></div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">关闭</el-button>
        <!-- <el-button :loading="rechargeLoading" size="mini" type="primary" @click="okRecharge()">确定</el-button> -->
      </div>
    </el-dialog>

    <el-dialog title="退保证金审核" :visible.sync="dialogCheckFormVisible" :close-on-click-modal="false" width="500px"
      @close="dialogCheckFormVisible = false">
      <el-form :model="checkForm" ref="checkForm" label-width="150px">
        <el-form-item label="操作类型" prop="type">
          <el-radio-group v-model="checkForm.type">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="reason">
          <common-input type="textarea" :autosize="{ minRows: 3, maxRows: 5 }" placeholder="拒绝审核时必填" maxlength="100"
            v-model="checkForm.reason" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogCheckFormVisible = false">取消</el-button>
        <el-button :loading="checkLoading" size="mini" type="primary" @click="check('checkForm')">确定</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import {
  glist,
  deletes,
  audit,
  details
} from '../../api/gift';
export default {
  components: {
    // 'pic-sel': () => import('../../components/PicSel/index')
  },
  data() {
    return {
      checkLoading: false,
      checkForm: {
        type: 1,
        reason: undefined,
        id: 0,
      },
      dialogCheckFormVisible: false,
      dialogFormVisible: false,
      rechargeLoading: false,
      list: [],
      listLoading: true,
      detail: {},
      listQuery: {
        page: 1,
        num: 20,
        keyword: undefined,
        status: '',
        contractEnd: undefined,
        memberKeyword: undefined
      },
      total: null,
      activeName: '0',
      rechargeForm: {
        sellerId: 0,
        money: undefined,
        note: undefined,
      }
    };
  },
  mounted() {
  },
  created() {
    this.getList();
  },

  methods: {
    getList() {
      this.listLoading = true;
      glist(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      })
    },
    //打开小店详情
    openShopDetail(id) {
      this.dialogFormVisible = true
      details(id).then(response => {
        this.detail = response.data;

      })
      //   router.push({path:'/'})
      //跳转小店详情
      // store.dispatch("setDetail", data);
      // this.$router.push({ path: "./shoplistDetail", query: { shopid: id } });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.num = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleClick(tab, event) {
      this.listQuery.status = this.activeName;
      this.handleFilter();
      // console.log(this.activeName)
    },
    leaveTab(activeName, oldActiveName) {
      // console.log(activeName)
      // return false;
    },
    handleCreate() {
      this.$router.push("/company/shop_create")
    },
    handleUpdate(row) {
      this.$router.push("/company/shop_edit?id=" + row.id)
    },
    getStatusColor(row) {
      if (row.status === 1) {
        return "success"
      }

      return "danger"
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deletes(row.id)
            .then(() => {
              this.$message.success("删除成功");
              const index = this.list.indexOf(row);
              this.list.splice(index, 1);
            });
        }).catch(() => { });
    },
    handleCheck(row) {
      this.checkForm.id = row.id;
      this.checkForm.type = 1;
      this.checkForm.reason = "";

      this.dialogCheckFormVisible = true;
    },
    handleCheck2(row) {
      this.$confirm('确定取消审核?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          seller_check2(row.id)
            .then(() => {
              this.$message.success("小店取消审核成功");
              this.getList();
            });
        }).catch(() => { });
    },
    handleRefuse(row) {
      this.$confirm('确定拒绝审核?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          seller_refuse(row.id)
            .then(() => {
              this.$message.success("小店拒绝审核成功");
              this.getList();
            });
        }).catch(() => { });
    },
    handleRecharge(row) {
      this.rechargeForm.sellerId = row.id;
      this.rechargeForm.money = undefined;
      this.rechargeForm.note = undefined;

      this.dialogFormVisible = true;
    },
    okRecharge() {
      if (typeof this.rechargeForm.money == undefined) {
        this.$message.error("请输入充值金额");
      }

      let money = $.trim(this.rechargeForm.money);
      if (money.length < 1) {
        this.$message.error("请输入充值金额");
      } else {
        money = Number(money) || 0;

        if (money == 0) {
          this.$message.error("请输入充值金额");
        } else {
          this.rechargeLoading = true;
          seller_recharge(this.rechargeForm).then((re) => {
            this.rechargeLoading = false;
            this.$message.success("成功充值");
            this.dialogFormVisible = false;
            this.getList();
          }).catch((err) => {
            this.rechargeLoading = false;
          })
        }
      }
    },
    check(formName) {
      this.checkForm.reason = $.trim(this.checkForm.reason)

      // console.log(this.form)
      if (this.checkForm.type == 2) {
        if (this.checkForm.reason.length < 1) {
          this.$message.error("拒绝审核时备注必填");
          return;
        }
      }
      this.checkLoading = true;
      audit(this.checkForm).then((res) => {
        this.checkLoading = false;
        this.$message.success("小店已审核");
        this.dialogCheckFormVisible = false;
        this.getList();
      }).catch((err) => {
        this.checkLoading = false;

      })
    }
  }
};
</script>
